<template>
	<view class="body">
		<view class="imags"><image src="../../static/image/vib_img.png" class="img"></image></view>
		<view class="inputadd"><input @focus="carNoShow" @blur="carNofalse" type="text" class="inp" placeholder="请输入车牌号"  v-model="license" /></view>
		<view><button type="default" class="addbtn" @click="addlicense">添加</button></view>
		<carAdd @getCarNo='getCarNo' @close="closeFn" v-if="isShow"  @del = 'delFn'/>

	</view>
</template>

<script>
import { addcar } from '../../utils/car/car.js';
import carAdd from '../../component/car/carAdd.vue'
export default  {
	components:{
		carAdd
	},
	data() {
		return {
			license: '',
			isShow:false
		};
	},
	onLoad() {},
	methods: {
		delFn(){
			if(this.license.length>0){
				this.license = this.license.substring(0,this.license.length-1)
			}
		},
		carNoShow(){
			this.isShow = true
		},
		getCarNo(val){
			this.license = val
		},
		closeFn(){
			this.isShow = false
		},
		async addlicense() {
			if(this.deteInp()) return;
			let license = this.license;
			const res = await addcar({ carno: license });
			uni.showModal({
				title: '提示',
				content: '是否添加该车牌号',
				success: function(res) {
					if (res.confirm) {
						uni.reLaunch({
							url: './addcar'
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		deteInp(){
			let license = this.license;
			let iflicense = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4,5}[A-Z0-9挂学警港澳]{1}$/;
			if (!iflicense.test(license)) {
				uni.showToast({
					title: '车牌号不合法',
					icon: 'error',
					mask: true
				});
				return true;
			}
		}
	}
};
</script>
<style lang="less">
@import './../../static/style/car.less';
</style>
